﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data;
using System.Data.SqlClient;

namespace RadarMonitor.DB
{
    /// <summary>
    /// 处理 插入和更新相关内容
    /// </summary>
    public partial class DBHelper
    {
        public event Action MonitorUpdate;

        /* 插入语法：
          1.
          sb.Append("INSERT Employees (Name, Location) ");
            sb.Append("VALUES (@name, @location);");
            sql = sb.ToString();
            using (SqlCommand command = new SqlCommand(sql, connection))
            {
                command.Parameters.AddWithValue("@name", "Jake");
                command.Parameters.AddWithValue("@location", "United States");
                int rowsAffected = command.ExecuteNonQuery();
                Console.WriteLine(rowsAffected + " row(s) inserted");
            }

            2. sqlStr = "insert into Borrow values('182056204','数据库','2020-05-02','15',null)";
            
            3. "INSERT INTO table (Column1, Column2) " + "Values ('string', 1)", myConnection);
            
         * 
         */


        public void insertBaseFTPRow(string file,int resultCode)
        {
            try
            {
                var str = $@"INSERT Table_BaseFtp (ResultCode) VALUES ({resultCode})";
                if(file != null)
                {
                    str = $@"INSERT Table_BaseFtp (LastedFile,ResultCode)  VALUES ('{file}',{resultCode})";
                }

          
                using (SqlCommand command = new SqlCommand(str, Connection))
                {
                    Connection.Open();
                    command.ExecuteNonQuery();
                }
            }
            catch(Exception e) {
                Console.WriteLine(e.ToString());
            }
            finally
            {
                try
                {
                    Connection.Close();
                }
                catch { }
            }
        }

        public void insertMonidorRow(int source, int AlarmCode,string Msg)
        {
            try
            {
                var str = $@"INSERT Table_Monidor (Source,AlarmCode)  VALUES ('{source}', {AlarmCode})";
                if(Msg != null)
                {
                    str = $@"INSERT Table_Monidor (Source,AlarmCode,Msg)  VALUES ({source}, {AlarmCode},N'{Msg}')";
                }
                using (SqlCommand command = new SqlCommand(str, Connection))
                {
                    Connection.Open();
                    command.ExecuteNonQuery();
                }
                Task.Run(() =>
                {
                    MonitorUpdate?.Invoke();
                });  
            }
            catch (Exception e)
            {
                Console.WriteLine(e.ToString());
            }
            finally
            {
                try
                {
                    Connection.Close();
                }
                catch { }
            }

        }

        public void insertProductsMonitorRow(string file, int resultCode)
        {
            try
            {
                var str = $@"INSERT Table_Products (ResultCode) VALUES ({resultCode})";
                if (file != null)
                {
                    str = $@"INSERT Table_Products (LastedFile,ResultCode)  VALUES ('{file}',{resultCode})";
                }


                using (SqlCommand command = new SqlCommand(str, Connection))
                {
                    Connection.Open();
                    command.ExecuteNonQuery();
                }
            }
            catch (Exception e)
            {
                Console.WriteLine(e.ToString());
            }
            finally
            {
                try
                {
                    Connection.Close();
                }
                catch { }
            }
        }

        public void insertBaseCimissRow(int resultCode)
        {
            try
            {
                var str = $@"INSERT Table_BaseCimiss (ResultCode) VALUES ({resultCode})";
                using (SqlCommand command = new SqlCommand(str, Connection))
                {
                    Connection.Open();
                    command.ExecuteNonQuery();
                }
            }
            catch (Exception e)
            {
                Console.WriteLine(e.ToString());
            }
            finally
            {
                try
                {
                    Connection.Close();
                }
                catch { }
            }
        }

        public void insertPingNetRow(int resultCode)
        {
            try
            {
                var str = $@"INSERT Table_Net (ResultCode) VALUES ({resultCode})";
                using (SqlCommand command = new SqlCommand(str, Connection))
                {
                    Connection.Open();
                    command.ExecuteNonQuery();
                }
            }
            catch (Exception e)
            {
                Console.WriteLine(e.ToString());
            }
            finally
            {
                try
                {
                    Connection.Close();
                }
                catch { }
            }
        }

        public void insertRdaRow(int code ,string description)
        {
            try
            {
                if (description == null)
                    description = "";
                var str = $@"INSERT Table_Rda (Code,Description) VALUES ({code},N'{description}')";
                using (SqlCommand command = new SqlCommand(str, Connection))
                {
                    Connection.Open();
                    command.ExecuteNonQuery();
                }
            }
            catch (Exception e)
            {
                Console.WriteLine(e.ToString());
            }
            finally
            {
                try
                {
                    Connection.Close();
                }
                catch { }
            }
        }
    }
}
